package response

import (
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
)

type R struct {
	Code ResCodeInt  `json:"code"`
	Msg  interface{} `json:"message"`
	Data interface{} `json:"data"`
}

func RSuccess(c *gin.Context, data interface{}) {
	rd := &R{
		Code: CodeSuccess,
		Msg:  CodeSuccess.Msg(),
		Data: data,
	}
	c.JSON(http.StatusOK, rd)
}

func RError(c *gin.Context, code ResCodeInt, msg ...string) {
	var resMsg = ""
	if len(msg) != 0 {
		resMsg = strings.Join(msg, ",")
	} else {
		resMsg = code.Msg()
	}

	rd := &R{
		Code: code,
		Msg:  resMsg,
		Data: nil,
	}
	c.JSON(http.StatusOK, rd)
}
